package com.indobrains.uud1945;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.indobrains.uud1945.database.DatabaseAdapter;

public class ChapterActivity extends ListActivity {
	private static final String TAG = "Chapter";
	private static final String CHAPTER_ID = "_id";
	private static final String CHAPTER_TITLE = "title";
	private DatabaseAdapter db;
	private Cursor cursor;
	private Intent intent;
	private Bundle bundle;
	private Dialog SearchDialog;
	private EditText keyword;
	private Button btnSearch;
	private static final String PAGE_NO="PAGE_NO";
	private static final String PAGE_TITLE="PAGE_TITLE";
	protected static final String SEARCH_KEYWORD = "_keyword";
	 
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		TextView title=(TextView)v.findViewById(R.id.title);
		TextView desc=(TextView)v.findViewById(R.id.description);
		String judul_bab=title.getText().toString()+" : "+desc.getText().toString();
		Intent intent=new Intent(this,Pasal2Activity.class);
    	Bundle bundle = new Bundle();
    	bundle.putInt(CHAPTER_ID, (int)id); //Your id
    	bundle.putString(CHAPTER_TITLE, judul_bab);
		intent.putExtras(bundle);
    	startActivity(intent);
    	//Log.d(TAG,"Chapter ID : "+Long.toString(id));
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chapter_layout);
		db=new DatabaseAdapter(this);
		db.Open();
		ListView lv=getListView();
		setChapterAdapter(lv);
		setTitle("Daftar BAB"); 
		
	}
	public void setChapterAdapter(ListView view){
		try {
			
			cursor=db.getAllChapter();
			
			String[] from = new String[] { db.CHAPTER_NAME,
					db.CHAPTER_DESC,db.CHAPTER_NOTE,db.CHAPTER_OLDER };
			int[] to = new int[] { R.id.title,R.id.description,R.id.note,R.id.old_version};
			//MenuAdapter adapter=new MenuAdapter(getActivity(), R.layout.list_item, cursor, from, to);
			ChapterAdapter adapter = new ChapterAdapter(this,R.layout.chapter_list,cursor,from,to);
			//DasarHukumAdapter adapter=new DasarHukumAdapter(this,R.layout.dasar_hukum,cursor,from,to);
			view.setAdapter(adapter);
			//setListAdapter(adapter);
		} catch (Exception ex){
			//Log.e(TAG,ex.getMessage());
		}
	}
	public void searchDialog(){
		SearchDialog=new Dialog(this);
		SearchDialog.setTitle("Masukkan kata kunci");
		SearchDialog.setContentView(R.layout.search_dialog);
		keyword=(EditText)SearchDialog.findViewById(R.id.keyword);
		btnSearch=(Button)SearchDialog.findViewById(R.id.btnSearch);
		
		btnSearch.setOnClickListener(search_button_click_listener);
		SearchDialog.show();
	}
	private OnClickListener search_button_click_listener = new OnClickListener() {
	    public void onClick(View v) {
	    	SearchDialog.dismiss();
	    	String s_keyword=keyword.getText().toString();
	    	intent=new Intent(ChapterActivity.this,SearchActivity.class);
	    	Bundle b=new Bundle();
	    	b.putString(SEARCH_KEYWORD, s_keyword);
	    	intent.putExtras(b);
	    	startActivity(intent);
	    	
	    }
	};
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()){
		case R.id.preface:
			intent=new Intent(this,ContentActivity.class);
	    	bundle = new Bundle();
	    	bundle.putInt(PAGE_NO, 0); //Your id
	    	bundle.putString(PAGE_TITLE, "Kata Pengantar");
			intent.putExtras(bundle);
	    	startActivity(intent);
	    	break;
		case R.id.preambule:
			intent=new Intent(this,ContentActivity.class);
			bundle = new Bundle();
			bundle.putInt(PAGE_NO, 1); //Your id
			bundle.putString(PAGE_TITLE, "Pembukaan");
			intent.putExtras(bundle);
	    	startActivity(intent); 
			break;
		case R.id.search:
			 searchDialog();
			break;
		case R.id.about:
			intent=new Intent(this,ContentActivity.class);
			bundle = new Bundle();
			bundle.putInt(PAGE_NO, 2); //Your id
			bundle.putString(PAGE_TITLE, "Tentang Program");
			intent.putExtras(bundle);
	    	startActivity(intent); 
			break;
		 
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.chapter_menu, menu);
		return true;
	}

}
